/*
 * (C) 1992 Simson Garfinkel and Associates, Inc.
 *
 * NeXTSTEP developers may freely use and redistribute this software as long
 * as credit is given to Simson Garfinkel and Associates.
 *
 * EXPORT RESTRICTIONS:
 *
 * You may not ship the source-code module des.c outside of the US or canada.
 *
 * You may ship a program which uses the des.o compiled module outside of the
 * United States to any type T or type V country as long as you do not provide
 * cryptographic services to the user in your program and you clearly
 * declare "commodity control number 5D11A" on your export declaration.
 *
 * Type T countries include all countries in the Western Hemisphere except Cuba.
 * Type V countries include all countries in the Eastern Hemisphere except
 * the previous communist block countries and the People's Republic of China,
 * Vietnam, Cambodia, and Laos.
 *
 * For further information, contact the Office of Export Control:
 *
 *	Bureau of Export Administration
 *	P.O. Box 273
 *	Washington, DC 20044
 *	202-377-2694
 */
 



#import <netdb.h>
#import <pwd.h>
#import <appkit/appkit.h>
#import <NXCType.h>
#import "Encoder.h"
#import "Registration.h"

static	int month(const char *m)
{
      if(!strcmp("January",m)) return 1;
      if(!strcmp("February",m)) return 2;
      if(!strcmp("March",m)) return 3;
      if(!strcmp("April",m)) return 4;
      if(!strcmp("May",m)) return 5;
      if(!strcmp("June",m)) return 6;
      if(!strcmp("July",m)) return 7;
      if(!strcmp("August",m)) return 8;
      if(!strcmp("September",m)) return 9;
      if(!strcmp("October",m)) return 10;
      if(!strcmp("November",m)) return 11;
      if(!strcmp("December",m)) return 12;
      return 0;
}

char	*calc_license_string(struct licenseString *lsIn,
			     const char *companyKey,
			     char *buf)
{
	struct	licenseString ls = *lsIn;
	unsigned char *cc = (unsigned char *)&ls;
	int	i;
	unsigned char counter;
	char	key[9];
	char 	ks[16][8];

	buf[0]	= '\000';
	
	/* calculate the checksum */
	counter 	= 0;
	ls.checksum	= 0;
	for(i=0;i<sizeof(struct licenseString);i++){
		counter += cc[i];
	}
	ls.checksum	= 256-counter;

	/* Now verify that it checksums to 0 */
	counter		= 0;
	for(i=0;i<sizeof(struct licenseString);i++){
		counter += cc[i];
	}
	if(counter!=0){
		NXRunAlertPanel(0,"Internal error in encryption",0,0,0);
		return 0;
	}

	asciiToKey(companyKey,key);
	desinit(0);
	dessetkey(ks,key);
	endes(ks,cc);
	endes(ks,cc+2);

	return binary_to_hex(&ls,sizeof(ls),buf);
}

@implementation Encoder

- nextLicense:sender
{
	[accessionNumberCell	setIntValue:[accessionNumberCell intValue]+1];
	[self	calcLicense:self];
	return self;
}

- calcLicense:sender
{
	BOOL	end = [endSwitch intValue];
	BOOL	start = [startSwitch intValue];
	char	buf[256];
	struct	licenseString ls;

	[startYearCover	setEnabled:start];
	[startMonthCover setEnabled:start];
	[endYearCover 	setEnabled:end];
	[endMonthCover	setEnabled:end];
	[simUsersCell	setEnabled:!strcmp([licenseTypeCover title],"Network")];
	if(![simUsersCell isEnabled]){
		[simUsersCell	setIntValue:1];
	}

	memset(&ls,0,sizeof(ls));
	ls.product	= [productCodeCell intValue];
	ls.num[0]	= ([accessionNumberCell intValue] >> 16) & 0xff;
	ls.num[1]	= ([accessionNumberCell intValue] >>  8) & 0xff;
	ls.num[2]	= ([accessionNumberCell intValue] >>  0) & 0xff;
	if(start){
		ls.start= (month([startMonthCover title]) << 4) +
		  			(atoi([startYearCover title]) - 1992);
		ls.flag |= REG_START;
	}
	if(end){
		ls.end=    (month([endMonthCover title]) << 4) +
		  			(atoi([endYearCover title]) - 1992);
		ls.flag |= REG_END;
	}

	ls.maxMachines[0]	= ([simUsersCell intValue] >> 8) & 0xff;
	ls.maxMachines[1]	= ([simUsersCell intValue] >> 0) & 0xff;
	
	if(!strcmp([licenseTypeCover title],"Single user")){
		ls.flag |= REG_SINGLEUSER;
	}

	if(!strcmp([licenseTypeCover title],"Demo")){
		ls.flag |= REG_DEMO;
	}

	if(!strcmp([companyKeyCell stringValue],"")){
		[licenseStringCell setStringValue:"Specify company key"];
		return self;
	}

	[licenseStringCell setStringValue:
	 calc_license_string(&ls,[companyKeyCell stringValue],buf)];

	return	 self;
}


- delayedCalcLicense:sender
{
	[self	perform:@selector(calcLicense:) with:nil afterDelay:(int)10
       		cancelPrevious:NO];
	return self;

}


@end

#import "RegSupport.m"
#import "des.c"
  
